package exttable.testselector;

import exttable.formgenerator.MessageFactory;
import exttable.testfiller.InputFormDisplay;
import exttable.view.ViewAnalysis;
import utils.Localisation;

import javax.swing.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jan 15, 2010
 * Time: 1:13:16 AM
 * To change this template use File | Settings | File Templates.
 */
public class TimeTableEditor extends DefaultCellEditor {
    private JButton jb;
    private int probationId;
    private int groupSchemaId;

    public TimeTableEditor(JCheckBox checkBox, int probationId, int groupSchemaId) {
        super(checkBox);
        this.probationId = probationId;
        this.groupSchemaId = groupSchemaId;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        ViewAnalysis va = (ViewAnalysis) table.getModel().getValueAt(row, 0);
        jb = new JButton(value.toString());
        jb.setName(row + "," + column);
        if (value.equals(Localisation.getMessage("timetable.result"))) {
            jb.setBackground(Color.GREEN);
            try {

                viewTestResultPane(va, probationId, groupSchemaId, column);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (value.equals(Localisation.getMessage("timetable.fill"))) {
            jb.setBackground(Color.RED);
            try {
                insertPanelRunner(va, probationId, groupSchemaId, column);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (value.equals(Localisation.getMessage("timetable.waiting"))) {
            jb.setBackground(Color.YELLOW);
            if (MessageFactory.areYouSure(Localisation.getMessage("tooltip.waiting")
                    + ", " + Localisation.getMessage("message.confirmSure"))) {
                try {
                    insertPanelRunner(va, probationId, groupSchemaId, column);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
        if (value.equals(Localisation.getMessage("timetable.noreq"))) {
            jb.setBackground(Color.GRAY);
            jb.setEnabled(false);
        }
        return jb;
    }

    private void insertPanelRunner(ViewAnalysis va, int probId, int groupSchId, int visit) throws Exception {
        new InputFormDisplay(va, probId, groupSchId, visit, null);
    }

    private void viewTestResultPane(ViewAnalysis analysis, int probId, int groupSchId, int visit) throws Exception {
        new ViewTestResults(analysis, probId, groupSchId, visit);
    }
}
